cap log close
clear all
set more off

* Diretório principal
cd "/Stata_moments_for_Matlab"

* Demais caminhos
global origdata "/Stata_moments_for_Matlab"
global savedata "/Stata_moments_for_Matlab"
global datamun  "/Stata_moments_for_Matlab"
global matlab 	"/Stata_moments_for_Matlab"


* Gravar log
log using moments_for_model.log, replace

******************************************************************
**# Criando variáveis e amostra na base principal
******************************************************************
{
	
* this data only has spouses and heads
use "$savedata/eneo_clean00_12_rev", clear

* Health indicator

bys id_hh quarter: egen any_hs_w = max(health_shock_w) 
bys id_hh quarter: egen any_hs_nw = max(health_shock_nw) 
gen any_hs = 0
replace any_hs = 1 if any_hs_w ==1 | any_hs_nw ==1 

* informality among the elderly

	tab situation1 if eda >= 65 & eda <= 70 & sex ==1, mi

	egen comb=group(year qt)
	egen time=rank(comb), by(idi)
	rename municip  mun_aux
	egen municip = concat(ent mun)
	drop mun_aux
	egen gr  =group(municip)
	su gr
	drop gr
	lab var municip "concat(ent mun)"
	*1396muns - OK
	
* count the number of families surveyed in a mun per quarter
	ge nfam = 1 if parentes == 1
	bys municip quarter: egen ntotfam = sum(nfam)
	lab var ntotfam "Number of families surveyed in a mun per quarter"
	* Retirado pela Rita em 21-23/04
	// 	drop nfam
	
* check the first year a mun appears in data
	ge newq = yq(year,qt)
	format newq %tq
	bys municip: egen minq = min(newq)
	format minq %tq
	lab var minq "First year a mun appears in data"
	lab var newq "Ano/quarter em formato dada"

* drop those municipalities that only entered in data after 2002
* about 600-700muns in ENE, 1000 in ENEO per quarter
	drop if minq > yq(2002,1)
	egen mungr = group(municip)
	su mungr
	lab var mungr "Identificador do município"

***Status: Unemployed + Inactive (1), Formal(2), Informal (3)
	tab clase1, m
	tab clase2, m
	tab clase1 clase2, m
	tab eda situation1 if time ==1

**Education group
	gen educationhigh=1 if  edu == 4 | edu == 3 
	replace educationhigh=0 if edu == 1 | edu == 2 
	lab var educationhigh "1 if complete secondary or more"

* Gráficos - Formal/Informal/Unemployed/Inactive - by gender
	preserve
	tab situation1, ge(dsit_)
	keep if educationhigh==0 & eda >= 20 & eda <= 59 & time == 1
	drop if newq == yq(2003,3) |  newq == yq(2003,4) 
	collapse dsit_* , by(newq sex) 
	label var dsit_1 "Unemployed + Inactive"
	label var dsit_2 "Formal"
	label var dsit_3 "Informal"
	line dsit_1 dsit_2 dsit_3 newq if sex == 1
		graph save "empcategory_homens", replace
		graph export "empcategory_homens.pdf", replace 
	line dsit_1 dsit_2 dsit_3 newq if sex == 2
		graph save "empcategory_mulheres", replace
		graph export "empcategory_mulheres.pdf", replace 
	restore
	
** create uniform variable for trabajador por cuenta propria (only for ocupados, ie, clase2== 1 or, equivalently, situation1=2 or 3)
ge 			self_employed = 1 if pos_ocu == 3 & year >= 2010 & year <= 2012 & situation1 >= 2 & situation1 <= 3
replace		self_employed = 0 if self_employed == . & year >= 2010 & year <= 2012 & situation1 >= 2 & situation1 <= 3

replace 	self_employed = 1 if o_pos_ocu == 3 & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3
replace 	self_employed = 1 if s_pos_ocu == 3 & self_employed == . & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3
replace		self_employed = 0 if self_employed == . & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3

** adding the empleadores to the self-employed
ge 			self_employed2 = 1 if (pos_ocu == 2 | pos_ocu == 3) & year >= 2010 & year <= 2012 & situation1 >= 2 & situation1 <= 3
replace		self_employed2 = 0 if self_employed == . & year >= 2010 & year <= 2012 & situation1 >= 2 & situation1 <= 3

replace 	self_employed2 = 1 if (o_pos_ocu == 2 | o_pos_ocu == 3) & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3
replace 	self_employed2 = 1 if (s_pos_ocu == 2 | s_pos_ocu == 3) & self_employed2 == . & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3
replace		self_employed2 = 0 if self_employed2 == . & year >= 2000 & year <= 2009 & situation1 >= 2 & situation1 <= 3

** self employed with insurance
tab self_employed insurance
tab self_employed2 insurance
tabstat self_employed self_employed2, by(insurance) stat(mean)

** check
tab self_employed situation1, m
tab self_employed2 situation1, m

** informality and self-employment
tabstat self_employed self_employed2, by(situation1) stat(mean)
tabstat self_employed self_employed2 if sex==1, by(situation1) stat(mean)
tabstat self_employed self_employed2 if sex==2, by(situation1) stat(mean)


************************************
****Job transitions: Quarterly******
************************************

************************************
* Only transitions across sectors
************************************

	tab situation1 time, mi

***Status for each month: first to fourth interview
	tab situation1, mi
	for num 1 2 3 4: gen auxX=situation1 if time == X
	egen situation_initial=max(aux1), by(idi)
	egen situation_second=max(aux2), by(idi)
	egen situation_third=max(aux3), by(idi)
	egen situation_fourth=max(aux4), by(idi)
	drop aux*

	compress 

**d10 =1 if Formal to Unemployed|Inactive
	gen d10m=0 if situation_initial==2 
	replace d10m=1 if situation_initial==2 & situation_second==1
	replace d10m=. if situation_initial==2 & situation_second==.
	lab var d10m "=1 if Formal to Unemployed|Inactive"

**d12 =1 if Formal to Informal
	gen d12m=0 if situation_initial==2 
	replace d12m=1 if situation_initial==2 & situation_second==3
	replace d12m=. if situation_initial==2 & situation_second==. 
	lab var d12m "=1 if Formal to Informal"

**d20 =1 if Informal to Unemployed|Inactive
	gen d20m=0 if situation_initial==3
	replace d20m=1 if situation_initial==3 & situation_second==1
	replace d20m=. if situation_initial==3 & situation_second==.   
	lab var d20m "=1 if Informal to Unemployed|Inactive"

**d21 =1 if Informal to Formal
	gen d21m=0 if situation_initial==3
	replace d21m=1 if situation_initial==3 & situation_second==2
	replace d21m=. if situation_initial==3 & situation_second==.
	lab var d21m "=1 if Informal to Formal"

*******************************************
******From Unemp|Inact transitions  ******
*******************************************

**d01 =1 if Unemployed|Inactive to Formal 
	gen d01m=0 if situation_initial==1 
	replace d01m=1 if situation_initial==1 & situation_second==2 
	replace d01m=. if situation_initial==1 & situation_second==. 
	lab var  d01m "=1 if Unemployed|Inactive to Formal "
	 
**d02 =1 if Unemployed|Inactive to Informal 
	gen d02m=0 if situation_initial==1 
	replace d02m=1 if situation_initial==1 & situation_second==3 
	replace d02m=. if situation_initial==1 & situation_second==. 
	lab var d02m "=1 if Unemployed|Inactive to Informal "

	compress 

*******************************************
* No transitions
*******************************************

**d11 =1 if Formal to Formal
	gen d11m=0 if situation_initial==2 
	replace d11m=1 if situation_initial==2 & situation_second==2
	replace d11m=. if situation_initial==2 & situation_second==.
	lab var d11m "=1 if Formal to Formal"

**d22 =1 if Informal to Informal
	gen d22m=0 if situation_initial==3 
	replace d22m=1 if situation_initial==3 & situation_second==3
	replace d22m=. if situation_initial==3 & situation_second==. 
	lab var d22m "=1 if Informal to Informal"

**d00 =1 if Unemployed|Inactive to Unemployed|Inactive
	gen d00m=0 if situation_initial==1
	replace d00m=1 if situation_initial==1 & situation_second==1
	replace d00m=. if situation_initial==1 & situation_second==.   
	lab var d00m "=1 if Unemployed|Inactive to Unemployed|Inactive"

	ge male = sex == 1	
	tab situation1, ge(dsit_)

	rename salaryd salaryd1
	

************************************	
*** Outros drops
	
** must have situation for each member in data
	egen gf = group(idi)
	su gf
	drop gf
	drop if situation1==. 
	egen gf = group(idi)
	su gf
	drop gf

** drop individuals that report to be employed, but which are not effectively working
	count if hrsocup ==0 & salaryd1 ==0 & (situation1==2 | situation1==3)
	count if hrsocup ==0 & salaryd1 ==0 & situation1==2 
	count if hrsocup ==0 & salaryd1 ==0 & situation1==3

	su salaryd1 if (situation1==2 | situation1==3) & hrsocup ==0
	su salaryd1 if situation1==2 & hrsocup ==0
	su salaryd1 if situation1==3 & hrsocup ==0

	drop if hrsocup ==0 & salaryd1 ==0 & (situation1==2 | situation1==3)
	egen gf = group(idi)
	su gf
	drop gf


** formal: must have at least min wage

	ge salnotrim =  salaryd1 if (situation1==2 | situation1==3) 
	ge wfinal = 3*salnotrim //convert monthly salary to quarterly
	su wfinal, d
	count if hrsocu ==0 & (situation1==2 | situation1==3) 
	lab var wfinal "Quarterly wage " 

	egen gf = group(idi)
	su gf
	drop gf

** drop individuals that earn less than minimum wage in formal sector 

	tab wagemin if situation1==2
	drop if wagemin == 1 & situation1==2
	egen gf = group(idi)
	su gf
	drop gf

** drop without labor inc in formal sector 

	drop if wagemin == 6 & situation1==2
	tab clase3 wagemin  if (situation1==2 | situation1==3)
	count if wfinal==0 & (situation1==2 | situation1==3)  & clase3 != 2

********* to check: how to incorporate in the model the 0 labor income in the model

	tab clase3 if situation1==3
	tab clase3 if situation1==3 & sex == 1
	tab clase3 if situation1==3 & sex == 2
	bys educationhigh: tab wagemin if situation1==3 & sex == 1 & parentes==1
	bys educationhigh: tab wagemin if situation1==3 & sex == 2 & parentes==2

* distribuion of hours worked for those without income in the informal sector
	su hrsocup wfinal if situation1==3 & sex == 1 & parentes==1 & clase3 == 2 , d
	su hrsocup wfinal if situation1==3 & sex == 2 & parentes==2 & clase3 == 2 , d

	ge nopaid =  clase3 == 2 if (situation1==2 | situation1==3)
	lab var nopaid "Without income in the informal sector"
	
* now generating husband and wife movements
* inclui variáveis novas sobre saúde

foreach var in nopaid hwaged situation1 situation_initial situation_second d01m d02m d10m d12m d20m ///
	d21m d00m d11m d22m educationhigh eda sex salaryd1 hrsocup wfinal self_employed self_employed2 {
	ge aux = `var' if parentes == 2 
	bys id_hh quarter: egen s_`var' = max(aux)
	drop aux
	replace s_`var' =. if married == 0
}

*** Age restriction para o Household Head
	egen testm = group(municip)
	su testm
	drop testm
	count
	keep if parentes == 1 & eda >= 20 & eda <= 59 
	drop if situation_initial == . | s_situation_initial ==. | situation_second == . | s_situation_second ==. 
	drop if educationhigh == . 
	tab year
	count
	tab s_sex if married == 1 & sex == 1
	tab sex if married == 1 
	lab var s_sex "Spouse é homem" 
	count

* traz os dados da base de municípios	
	ge st = ent
	sort st mun
	merge st mun using "$datamun/mun_rollout_data_simple.dta"
	tab _merge
	keep if _merge == 3
	drop _merge	

** deixa apenas 1 linha por hh = casados / homem
	egen gf = group(idi)
	su gf
	drop gf
	tab married 
	keep if married ==1 

	egen gf = group(idi)
	su gf
	tab sex 
	drop if sex == 2
	drop gf
	tab s_sex sex, m

* identificação do município com SP e tempo desde a implementação	
	tab1 situation_initial s_situation_initial, m
	egen testm = group(municip)
	su testm
	drop testm
	ge dsp = qentry_newc10 <= newq
	lab var dsp "Municipality implemented SP"
	ge tsince = newq-qentry_newc10
	format qentry_newc10 %tq

	ge period = 1 if tsince <= -1 
	replace period = 2 if tsince >= 0 & tsince <. 


***** Top trimming 
* max income of women and men

	
	*informal, head
	drop if wfinal > 36358 & wfinal !=. & situation1==3
	* 107,700 observations deleted na base original - 107,706 agora, ok
	*informal, spouse
	drop if s_wfinal > 36358 & s_wfinal !=. & s_situation1==3 
	*9,755 observations deleted na base original - ok

	*formal, head
	drop if wfinal > 36358 & wfinal !=. & situation1==2 
	*138,961 observations deleted na base original - 138,966 agora, ok
	*formal, spouse
	drop if s_wfinal > 36358 & s_wfinal !=. & s_situation1==2 
	*15,542 observations deleted - ok 

	count if wfinal ==. & situation1 !=1
	su wfinal if situation1==3, d
	su wfinal if situation1==2, d
	
*check wage of informal --- non remunerado! 
	count if wfinal == 0 & clase3 == 2 &  (situation1==2 | situation1==3)
	count if wfinal == 0 & clase3 == 2 &  situation1==2 
	count if wfinal == 0 & clase3 == 2 &  situation1==3

	count if wfinal == . & clase3 == 2 &  (situation1==2 | situation1==3)
	count if wfinal == . & clase3 == 2 &  situation1==2 
	count if wfinal == . & clase3 == 2 &  situation1==3

* Todo o bloco até salvar as bases
	drop minq
	bys municip: egen minq = min(newq)
	format minq %tq
	tab minq
	drop if minq > yq(2002,4)
	drop minq
	egen testm = group(municip)
	su testm
	drop testm

	tab time
	bys idi (quarter): ge n = _n
	bys idi (quarter): ge n2 = _N
	tab n2 if n==1
	drop n n2
	keep if time <= 2
	count

	egen testm = group(municip)
	su testm
	drop testm
	
	* stocks
	ge mff = situation1 == 2 & s_situation1 == 2
	ge mfn = situation1 == 2 & s_situation1 == 1 
	ge mfi = situation1 == 2 & s_situation1 == 3
	ge formalh4 = situation1 == 2 & (s_situation1 == 1 | s_situation1 == 3)

	ge mif = situation1 == 3 & s_situation1 == 2
	ge mnf = situation1 == 1 & s_situation1 == 2

	ge min_ = situation1 == 3 & s_situation1 == 1 
	ge mii = situation1 == 3 & s_situation1 == 3
	ge mnn = situation1 == 1 & s_situation1 == 1

	ge mni = situation1 == 1 & s_situation1 == 3

	ge informalh= situation1 != 2 & s_situation1 !=2
	ge formalh= situation1 == 2 | s_situation1 ==2
	
** prepare data to send to matlab

* Transitions from the 1st to 4th month.	
	ge dnf_1 = d01m
	ge dni_1 = d02m
	ge dfn_1 = d10m
	ge din_1 = d20m
	ge dfi_1 = d12m
	ge dif_1 = d21m
	ge dii_1 = d22m
	ge dff_1 = d11m
	ge dni_df_1 = s_d02m if d10m == 1
	replace dni_df_1 = 0 if d10m == 1 & s_d02m  ==.
	ge dni_di_1 = s_d02m if d20m == 1
	replace dni_di_1 =0 if  d20m == 1 & s_d02m  ==. 

	ge dnf_2 = s_d01m
	ge dni_2 = s_d02m
	ge dfn_2 = s_d10m
	ge din_2 = s_d20m
	ge dfi_2 = s_d12m
	ge dif_2 = s_d21m
	ge dii_2 = s_d22m
	ge dff_2 = s_d11m
	ge dni_df_2 = d02m if s_d10m == 1
	replace dni_df_2 = 0 if s_d10m == 1 &  d02m == .
	ge dni_di_2 = d02m if s_d20m == 1 
	replace dni_di_2 = 0 if s_d20m == 1 &  d02m == .

	replace hrsocup = . if hrsocup ==0
	replace s_hrsocup = . if s_hrsocup ==0	
	
** drop 0 wages - they are not used in model
foreach num in 1 2 {
	ge wfinal`num' = wfinal if time == `num'
	ge s_wfinal`num' = s_wfinal if time == `num'
}
	rename wfinal2 aux
	bys idi: egen  wfinal2 = max(aux)
	drop aux
	rename s_wfinal2 aux
	bys idi: egen  s_wfinal2 = max(aux)
	drop aux

	count if wfinal == 0 & situation1==2
	count if wfinal == 0 & situation1==3
	count if s_wfinal == 0 & s_situation1==2
	count if s_wfinal == 0 & s_situation1==3
	su  wfinal* if (situation1==2 | situation1==3)
	su s_wfinal if (s_situation1==2 | s_situation1==3)
	drop if wfinal == 0 & (situation1==2 | situation1==3)
	drop if s_wfinal == 0 & (s_situation1==2 | s_situation1==3)

** trim bottom 5% of informal wages
*informal
_pctile wfinal if situation1==3, p(5) 
drop if wfinal < r(r1) & wfinal !=0 & situation1==3
_pctile s_wfinal if s_situation1==3, p(5) 
drop if s_wfinal < r(r1) & s_wfinal !=0 & s_situation1==3

	for num 1 2: ge lnX =ln(wfinalX)
	for num 1 2: ge s_lnX =ln(s_wfinalX)

	egen gf = group(idi)
	su gf
	drop gf

	su wfinal* if time == 1 & (situation1==2 | situation1==3)
	su wfinal* if time == 2 & (situation1==2 | situation1==3)
	su s_wfinal* if time == 1 & (s_situation1==2 | s_situation1==3)
	su s_wfinal* if time == 2 & (s_situation1==2 | s_situation1==3)

** families must be present at 1st and 2nd interviews
	tab time
	ge d = informalh !=.
	bys idi: egen sum= sum(d)
	tab sum
	keep if sum == 2
	drop d sum	
	
* Save	
	save "$savedata/sample_final", replace
	log close

	keep if time == 1
	
* Salario head
gen wi_1= wfinal1 if situation_initial == 3 & wfinal1!= .
gen wf_1= wfinal1 if situation_initial == 2 & wfinal1!= .
* Salario spouse
gen wi_2= s_wfinal1 if s_situation_initial == 3 & s_wfinal1!= .
gen wf_2= s_wfinal1 if s_situation_initial == 2 & s_wfinal1!= .


	save "$savedata/sample_formodel", replace

clear

}

**********************************************************
**# Extração dos dados para Matlab

* Base
	use "$savedata/sample_formodel", clear
	

tabstat wfinal1 s_wfinal1, by(year) stat(min max)
	
*** Extração Original
{
		
local i = 0
foreach yt in  1  2  {					
		foreach ed in 0 1  { 
			foreach comp in 0 1  {			/*with children in hh or not*/
				preserve
				di "educationhigh `ed' with children `comp' period `yt'"
				keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt'
				
				*head					
				*Distributions of wages in the first interview
				su wfinal1 if situation_initial == 3 & wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_i_1 = .
				ge fw_i_1 = .
			
				
				ge w_i0_1 =  min   if _n == 1
				qui ge counter = 1 if  (wfinal1!= . & wfinal1< min + d) & situation_initial == 3 
				egen counter_b = sum(counter)
				replace gw_i_1 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if situation_second == 3 & (wfinal2 != . & wfinal2 < min + d) & situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_i_1 = counter_b if _n == 1
				drop counter*
				
				local m = max
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_i0_1 =  w_i0_1[`k']+d if _n == `j'
					qui ge counter = 1 if (wfinal1!= . & wfinal1>=  min + `k'*d & wfinal1< min + `j'*d) & situation_initial == 3 
					egen counter_b = sum(counter)
					replace gw_i_1 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if situation_second == 3 & (wfinal2 != . & wfinal2 >=  min + `k'*d & wfinal2 < min + `j'*d) & situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_i_1 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
				
				su wfinal1 if situation_initial == 2 & wfinal1!= ., d  
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_f_1 = . 
				ge fw_f_1 = .
				
				ge w_f0_1 = min if _n == 1
				qui ge counter = 1 if (wfinal1!= . & wfinal1< min + d) & situation_initial == 2 
				egen counter_b = sum(counter)
				replace gw_f_1 = counter_b if _n == 1
				drop counter*
				qui ge counter = 1 if situation_second == 2 & (wfinal2 != . & wfinal2 < min + d) & situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_f_1 = counter_b if _n == 1
				drop counter*
				
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_f0_1 = w_f0_1[`k']+d if _n == `j'
					qui ge counter = 1 if (wfinal1!= . & wfinal1>=  min + `k'*d & wfinal1< min + `j'*d) & situation_initial == 2
					egen counter_b = sum(counter)
					replace gw_f_1 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if situation_second == 2 & (wfinal2 != . & wfinal2 >=  min + `k'*d & wfinal2 < min + `j'*d)  & situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_f_1 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
						
				* spouse					
				*Distributions of wages in the first interview
				su s_wfinal1 if s_situation_initial == 3 & s_wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_i_2= .
				ge fw_i_2 = .
			
			
				ge w_i0_2 = min  if _n == 1
				qui ge counter = 1 if  (s_wfinal1!= . & s_wfinal1< min + d) & s_situation_initial == 3 
				egen counter_b = sum(counter)
				qui replace gw_i_2 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if s_situation_second == 3 & (s_wfinal2 != . & s_wfinal2 < min + d) & s_situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_i_2 = counter_b if _n == 1
				drop counter*
	
	
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_i0_2 = w_i0_2[`k']+d if _n == `j'
					qui ge counter = 1 if (s_wfinal1!= . & s_wfinal1>=  min + `k'*d & s_wfinal1< min + `j'*d) & s_situation_initial == 3 
					egen counter_b = sum(counter)
					qui replace gw_i_2 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if s_situation_second == 3 & (s_wfinal2 != . & s_wfinal2 >=  min + `k'*d & s_wfinal2 < min + `j'*d) & s_situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_i_2 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1				
					
				}
				drop max min d 
				
				su s_wfinal1 if s_situation_initial == 2 & s_wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_f_2 =.
				ge fw_f_2 =.
				
				qui ge counter = 1 if  (s_wfinal1!= . & s_wfinal1< min + d) & s_situation_initial == 2 
				egen counter_b = sum(counter)
				qui replace gw_f_2 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if s_situation_second == 2 & (s_wfinal2 != . & s_wfinal2 < min + d) & s_situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_f_2 = counter_b if _n == 1
				drop counter*
				
				ge w_f0_2 = min   if _n == 1
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_f0_2 = w_f0_2[`k']+d if _n == `j'
					qui ge counter = 1 if (s_wfinal1!= . & s_wfinal1>=  min + `k'*d & s_wfinal1< min + `j'*d) & s_situation_initial == 2
					egen counter_b = sum(counter)
					qui replace gw_f_2 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if s_situation_second == 2 & (s_wfinal2 != . & s_wfinal2 >=  min + `k'*d & s_wfinal2 < min + `j'*d) & s_situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_f_2 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
							
											
						
				qui compress
				save "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'.dta", replace

				foreach W in f i {
					foreach S in 1 2 {
					** prepare files to matlab			
					use "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'.dta", clear
					collapse (sum) fw_`W'_`S' gw_`W'_`S' if w_`W'0_`S' != ., by(w_`W'0_`S')
					egen sumf=sum(fw_`W'_`S')
					egen sumg=sum(gw_`W'_`S')
					replace fw_`W'_`S' = fw_`W'_`S'/sumf
					replace gw_`W'_`S' = gw_`W'_`S'/sumg
					drop sumf sumg
					save "$matlab/FG`W'_time`yt'_age`i'_eh`ed'_comp`comp'_spouse`S'.dta", replace
					}
				}
				
				
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using "$savedata/sample_formodel", clear												
				collapse (mean) mean_wi_1=wi_1 mean_wf_1=wf_1 mean_wi_2=wi_2 mean_wf_2=wf_2 (sd) sd_wi_1=wi_1 sd_wf_1=wf_1 sd_wi_2=wi_2 sd_wf_2=wf_2 /*
				*/ (p10) p10_wi_1=wi_1 p10_wf_1=wf_1 p10_wi_2=wi_2 p10_wf_2=wf_2 (p25) p25_wi_1=wi_1 p25_wf_1=wf_1 p25_wi_2=wi_2 p25_wf_2=wf_2 /*
				*/ (p50) p50_wi_1=wi_1 p50_wf_1=wf_1 p50_wi_2=wi_2 p50_wf_2=wf_2 (p75) p75_wi_1=wi_1 p75_wf_1=wf_1 p75_wi_2=wi_2 p75_wf_2=wf_2 /*
				*/ (p90) p90_wi_1=wi_1 p90_wf_1=wf_1 p90_wi_2=wi_2 p90_wf_2=wf_2 (min) min_wi_1=wi_1 min_wf_1=wf_1 min_wi_2=wi_2 min_wf_2=wf_2 (max) max_wi_1=wi_1 max_wf_1=wf_1 max_wi_2=wi_2 max_wf_2=wf_2
				outsheet _all using "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'", nolabel  nonames replace
				
							
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using "$savedata/sample_formodel", clear
				su  dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
				collapse (mean) dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2										
				outsheet _all using "$matlab/trans_time`yt'_age`i'_eh`ed'_comp`comp'", nolabel  nonames replace
				
							
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' using "$savedata/sample_formodel", clear
				su mff mfi mfn mif mnf mii min_ mni mnn
				collapse (mean) mff mfi mfn mif mnf mii min_ mni mnn								
				outsheet _all using "$matlab/stock_time`yt'_age`i'_eh`ed'_comp`comp'", nolabel  nonames replace

				use "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_spouse1.dta", clear
				merge using "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_spouse1.dta"
				drop _merge
				merge using "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_spouse2.dta"
				drop _merge
				merge using "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_spouse2.dta"
				drop _merge
				save "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'1.dta", replace
				
				erase "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_spouse1.dta"
				erase "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_spouse1.dta"
				erase "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_spouse2.dta"
				erase "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_spouse2.dta"
				
				order w_f0_1 w_i0_1 gw_f_1 gw_i_1 fw_f_1 fw_i_1 w_f0_2 w_i0_2 gw_f_2 gw_i_2 fw_f_2 fw_i_2 
				outsheet _all using "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'1", nolabel  nonames replace

				erase "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'1.dta"
				erase "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'.dta"

				restore
			}
		}
	}
}

*** Extração por status de saúde

{
local i = 0
foreach yt in  1  2  {	
	foreach gru in  0  1  {				/* any_hs */		
		foreach ed in 0 1  { 
			foreach comp in 0 1  {			/*with children in hh or not*/
				preserve
				di "educationhigh `ed' with children `comp' period `yt' any_hs `gru'"
				keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru'
				
				*head					
				*Distributions of wages in the first interview
				su wfinal1 if situation_initial == 3 & wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_i_1 = .
				ge fw_i_1 = .
			
				
				ge w_i0_1 =  min   if _n == 1
				qui ge counter = 1 if  (wfinal1!= . & wfinal1< min + d) & situation_initial == 3 
				egen counter_b = sum(counter)
				replace gw_i_1 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if situation_second == 3 & (wfinal2 != . & wfinal2 < min + d) & situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_i_1 = counter_b if _n == 1
				drop counter*
				
				local m = max
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_i0_1 =  w_i0_1[`k']+d if _n == `j'
					qui ge counter = 1 if (wfinal1!= . & wfinal1>=  min + `k'*d & wfinal1< min + `j'*d) & situation_initial == 3 
					egen counter_b = sum(counter)
					replace gw_i_1 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if situation_second == 3 & (wfinal2 != . & wfinal2 >=  min + `k'*d & wfinal2 < min + `j'*d) & situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_i_1 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
				
				su wfinal1 if situation_initial == 2 & wfinal1!= ., d  
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_f_1 = . 
				ge fw_f_1 = .
				
				ge w_f0_1 = min if _n == 1
				qui ge counter = 1 if (wfinal1!= . & wfinal1< min + d) & situation_initial == 2 
				egen counter_b = sum(counter)
				replace gw_f_1 = counter_b if _n == 1
				drop counter*
				qui ge counter = 1 if situation_second == 2 & (wfinal2 != . & wfinal2 < min + d) & situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_f_1 = counter_b if _n == 1
				drop counter*
				
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_f0_1 = w_f0_1[`k']+d if _n == `j'
					qui ge counter = 1 if (wfinal1!= . & wfinal1>=  min + `k'*d & wfinal1< min + `j'*d) & situation_initial == 2
					egen counter_b = sum(counter)
					replace gw_f_1 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if situation_second == 2 & (wfinal2 != . & wfinal2 >=  min + `k'*d & wfinal2 < min + `j'*d)  & situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_f_1 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
						
				* spouse					
				*Distributions of wages in the first interview
				su s_wfinal1 if s_situation_initial == 3 & s_wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_i_2= .
				ge fw_i_2 = .
			
			
				ge w_i0_2 = min  if _n == 1
				qui ge counter = 1 if  (s_wfinal1!= . & s_wfinal1< min + d) & s_situation_initial == 3 
				egen counter_b = sum(counter)
				qui replace gw_i_2 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if s_situation_second == 3 & (s_wfinal2 != . & s_wfinal2 < min + d) & s_situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_i_2 = counter_b if _n == 1
				drop counter*
	
	
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_i0_2 = w_i0_2[`k']+d if _n == `j'
					qui ge counter = 1 if (s_wfinal1!= . & s_wfinal1>=  min + `k'*d & s_wfinal1< min + `j'*d) & s_situation_initial == 3 
					egen counter_b = sum(counter)
					qui replace gw_i_2 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if s_situation_second == 3 & (s_wfinal2 != . & s_wfinal2 >=  min + `k'*d & s_wfinal2 < min + `j'*d) & s_situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_i_2 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1				
					
				}
				drop max min d 
				
				su s_wfinal1 if s_situation_initial == 2 & s_wfinal1!= ., d
				qui ge max = r(max)
				qui ge min = r(min)
				local np = 50
				ge d = (max-min)/(`np'-1)
				ge gw_f_2 =.
				ge fw_f_2 =.
				
				qui ge counter = 1 if  (s_wfinal1!= . & s_wfinal1< min + d) & s_situation_initial == 2 
				egen counter_b = sum(counter)
				qui replace gw_f_2 = counter_b if _n == 1
				drop counter*

				qui ge counter = 1 if s_situation_second == 2 & (s_wfinal2 != . & s_wfinal2 < min + d) & s_situation_initial == 1
				egen counter_b = sum(counter)
				replace fw_f_2 = counter_b if _n == 1
				drop counter*
				
				ge w_f0_2 = min   if _n == 1
				local m = max 
				local k = 1
				while `k' < `np' {
					local j = `k' + 1
					di `k' `j'
					replace  w_f0_2 = w_f0_2[`k']+d if _n == `j'
					qui ge counter = 1 if (s_wfinal1!= . & s_wfinal1>=  min + `k'*d & s_wfinal1< min + `j'*d) & s_situation_initial == 2
					egen counter_b = sum(counter)
					qui replace gw_f_2 = counter_b if _n == `j'
					drop counter*
					
					qui ge counter = 1 if s_situation_second == 2 & (s_wfinal2 != . & s_wfinal2 >=  min + `k'*d & s_wfinal2 < min + `j'*d) & s_situation_initial == 1
					egen counter_b = sum(counter)
					replace fw_f_2 = counter_b if _n == `j'
					drop counter*
					
					local k = `k' + 1
					
				}
				drop max min d 
							
											
						
				qui compress
				save "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'.dta", replace

				foreach W in f i {
					foreach S in 1 2 {
					** prepare files to matlab			
					use "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'.dta", clear
					collapse (sum) fw_`W'_`S' gw_`W'_`S' if w_`W'0_`S' != ., by(w_`W'0_`S')
					egen sumf=sum(fw_`W'_`S')
					egen sumg=sum(gw_`W'_`S')
					replace fw_`W'_`S' = fw_`W'_`S'/sumf
					replace gw_`W'_`S' = gw_`W'_`S'/sumg
					drop sumf sumg
					save "$matlab/FG`W'_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse`S'.dta", replace
					}
				}
				
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using "$savedata/sample_formodel", clear												
				collapse (mean) mean_wi_1=wi_1 mean_wf_1=wf_1 mean_wi_2=wi_2 mean_wf_2=wf_2 (sd) sd_wi_1=wi_1 sd_wf_1=wf_1 sd_wi_2=wi_2 sd_wf_2=wf_2 /*
				*/ (p10) p10_wi_1=wi_1 p10_wf_1=wf_1 p10_wi_2=wi_2 p10_wf_2=wf_2 (p25) p25_wi_1=wi_1 p25_wf_1=wf_1 p25_wi_2=wi_2 p25_wf_2=wf_2 /*
				*/ (p50) p50_wi_1=wi_1 p50_wf_1=wf_1 p50_wi_2=wi_2 p50_wf_2=wf_2 (p75) p75_wi_1=wi_1 p75_wf_1=wf_1 p75_wi_2=wi_2 p75_wf_2=wf_2 /*
				*/ (p90) p90_wi_1=wi_1 p90_wf_1=wf_1 p90_wi_2=wi_2 p90_wf_2=wf_2 (min) min_wi_1=wi_1 min_wf_1=wf_1 min_wi_2=wi_2 min_wf_2=wf_2 (max) max_wi_1=wi_1 max_wf_1=wf_1 max_wi_2=wi_2 max_wf_2=wf_2
				outsheet _all using "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace
					
							
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using "$savedata/sample_formodel", clear
				su  dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2	
				collapse (mean) dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2										
				outsheet _all using "$matlab/trans_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace
				
							
				use if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & any_hs == `gru' using "$savedata/sample_formodel", clear
				su mff mfi mfn mif mnf mii min_ mni mnn
				gen mfx_=mff+mfi+mfn            /*generating aggregated moments to be used only for anyhs = 1 (due to lack of size) */
				gen mix=mif+mii+min_
				collapse (mean) mff mfi mfn mif mnf mii min_ mni mnn mfx_ mix							
				outsheet _all using "$matlab/stock_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'", nolabel  nonames replace

				use "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse1.dta", clear
				merge using "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse1.dta"
				drop _merge
				merge using "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse2.dta"
				drop _merge
				merge using "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse2.dta"
				drop _merge
				save "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'1.dta", replace
				
				erase "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse1.dta"
				erase "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse1.dta"
				erase "$matlab/FGf_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse2.dta"
				erase "$matlab/FGi_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'_spouse2.dta"
				
				order w_f0_1 w_i0_1 gw_f_1 gw_i_1 fw_f_1 fw_i_1 w_f0_2 w_i0_2 gw_f_2 gw_i_2 fw_f_2 fw_i_2 
				outsheet _all using "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'1", nolabel  nonames replace

				erase "$matlab/W_time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'1.dta"
				erase "$matlab/time`yt'_age`i'_eh`ed'_comp`comp'_anyhs`gru'.dta"

				restore
			}
		}		
	}
}
}

tab any_hs if dchild0_14 == 1 &  educationhigh == 0 & period == 1 
tab any_hs if dchild0_14 == 1 &  educationhigh == 0 & period == 2
tab any_hs if dchild0_14 == 1 &  educationhigh == 1 & period == 1 
tab any_hs if dchild0_14 == 1 &  educationhigh == 1 & period == 2

tab any_hs dchild0_14, mi
tab any_hs period if educationhigh==0 & dchild0_14 == 1 , mi
tab any_hs period if educationhigh==1 & dchild0_14 == 1 , mi

*---------------------------------------------------------------------------------------------------
**# Matrix - health transition probabilities, and unhealthy household proportion
*---------------------------------------------------------------------------------------------------	
{
	* Base
	use  "$savedata/sample_final", clear

** Check de domicílios com mais ou menos do que 2 obs
// 	sort id_hh 
// 	br id_hh year quarter period time any_hs period tsince
	bysort id_hh : egen num = count(id_hh)
	tab num time, mi
	keep if num == 2 	
	drop num

* Indicador de saúde nas duas observações
	gen testa=any_hs if time==1
	gen testb=any_hs if time==2
	bysort id_hh: egen any_hs1 = max(testa)
	bysort id_hh: egen any_hs2 = max(testb)
	drop testa testb
	
* Matriz - teste	
// 	tab any_hs1 any_hs2 if educationhigh == 0 & time == 1 & dchild0_14 == 1 & period == 1, miss 
	tab any_hs1 any_hs2 if educationhigh == 1 & time == 1 & dchild0_14 == 1 & period == 1, miss 
// 	tab any_hs1 any_hs2 if educationhigh == 0 & time == 1 & dchild0_14 == 1 & period == 2, miss 
// 	tab any_hs1 any_hs2 if educationhigh == 1 & time == 1 & dchild0_14 == 1 & period == 2, miss 
	
* variáveis
	gen hea_un = 1 if any_hs1 == 0 & any_hs2 ==1
	gen un_hea = 1 if any_hs1 == 1 & any_hs2 ==0
	gen num = 1
	gen health_t = 1 if any_hs1 == 0
	gen unhealth_t = 1 if any_hs1 == 1

	
*** Extração
* Com/sem filho, educ low/hig, period antes/depois = 8 grupos/arquivos 	

foreach yt in  1  2  {					
	foreach ed in 0 1  { 
		foreach comp in 0 1  {			/*with children in hh or not*/
			preserve
			di "educationhigh `ed' with children `comp' period `yt'"
			keep if dchild0_14 == `comp' &  educationhigh == `ed' & period == `yt' & time == 1
			collapse (sum) health_t unhealth_t num hea_un un_hea
			gen phu = hea_un / health_t
			gen puh = un_hea / unhealth_t
			gen probu = unhealth_t / num
			keep phu puh probu
			outsheet _all using "$matlab/matrix_time`yt'_age0_eh`ed'_comp`comp'", nolabel  nonames replace
			restore			
			}	
		}
}
	
clear

}

